#include "ObjectPool.h"
#include "ConcurrentAlloc.h"
#include <vector>

void Alloc1()
{	
	std::vector<void*> vec;
	vec.reserve(5);
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		std::cout << ptr << std::endl;
		vec.push_back(ptr);
	}
	for (size_t i = 0; i < 5; ++i)
	{
		ConcurrentFree(vec[i]);
	}
}

void Alloc2()
{
	std::vector<void*> vec;
	vec.reserve(5);
	for (size_t i = 0; i < 5; ++i)
	{
		void* ptr = ConcurrentAlloc(7);
		std::cout << ptr << std::endl;
		vec.push_back(ptr);
	}
	for (size_t i = 0; i < 5; ++i)
	{
		ConcurrentFree(vec[i]);
	}
}

void TLSTest()
{
	std::thread t1(Alloc1);
	t1.join();

	std::thread t2(Alloc2);
	t2.join();
}

void TestConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(6); 
	void* p2 = ConcurrentAlloc(1);
	void* p3 = ConcurrentAlloc(7);
	void* p4 = ConcurrentAlloc(8);
	//void* p5 = ConcurrentAlloc(1);
	//void* p6 = ConcurrentAlloc(2);
	//void* p7 = ConcurrentAlloc(3);


	cout << p1 << endl;
	cout << p2 << endl;
	cout << p3 << endl;
	cout << p4 << endl;
	//cout << p5 << endl;
	//cout << p6 << endl;
	//cout << p7 << endl;

	ConcurrentFree(p1);
	ConcurrentFree(p2);		
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	//ConcurrentFree(p5, 1);
	//ConcurrentFree(p6, 2);
	//ConcurrentFree(p7, 3);


}

void TestConcurrentAlloc2()
{
	for (size_t i = 0; i < 1024; ++i)
	{
		void* p1 = ConcurrentAlloc(6);
		cout << p1 << endl;
	}
	void* p2 = ConcurrentAlloc(8);
	cout << p2 << endl;
}

void MutiThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(6);
		v.push_back(ptr);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void MutiThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; ++i)
	{
		void* ptr = ConcurrentAlloc(15);
		v.push_back(ptr);
	}
	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void TestMutiThread()
{
	std::thread t1(MutiThreadAlloc1);
	std::thread t2(MutiThreadAlloc2);
	
	t1.join();
	t2.join();
}

//int main()
//{
//	//TLSTest();
//	//TestConcurrentAlloc1();
//	//TestConcurrentAlloc2();
//
//	TestMutiThread();
//
//	return 0;
//}
